package csu.webcourse.mypetstore.web.servlet;

import csu.webcourse.mypetstore.domain.Account;
import csu.webcourse.mypetstore.domain.Product;
import csu.webcourse.mypetstore.service.AccountService;
import csu.webcourse.mypetstore.service.CategoryService;
import csu.webcourse.mypetstore.service.LogService;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class SignonServlet extends HttpServlet {
    private static final String SIGNON_FORM = "/WEB-INF/jsp/account/signon.jsp";

    private String username;
    private String password;
    private String msg;

    private String checkCode;
    private String checkCode1;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");
        this.password = req.getParameter("password");
//获取用户输入的验证码

        checkCode1 = req.getParameter("checkCode");
//程序生成的验证码，用session获取
        HttpSession session1 = req.getSession();
        checkCode = (String) session1.getAttribute("checkCode");

        //检验用户输入正确性
            if (!validate()) {
                req.setAttribute("signonMsg", this.msg);

                HttpSession session = req.getSession();
                Account account = (Account) session.getAttribute("loginAccount");
                if (account != null) {
                    HttpServletRequest httpRequest = req;
                    String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                            + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                    LogService logService = new LogService();
                    //最后加入的信息“XXXXX”应当为该界面的信息以及一些商品信息
                    String logInfo = logService.logInfo(" ") + strBackUrl + "用户填写登录信息有误";
                    logService.insertLogInfo(account.getUsername(), logInfo);
                }

                req.getRequestDispatcher(SIGNON_FORM).forward(req, resp);
            } else {
                AccountService accountService = new AccountService();
                Account loginAccount = accountService.getAccount(username, password);
                if (loginAccount == null) {
                    this.msg = "用户名或密码错误";
                    req.getRequestDispatcher(SIGNON_FORM).forward(req, resp);
                } else {
                    loginAccount.setPassword(null);
                    HttpSession session = req.getSession();
                    session.setAttribute("loginAccount", loginAccount);

                    if (loginAccount.isListOption()) {
                        CategoryService categoryService = new CategoryService();
                        List<Product> myList = categoryService.getProductListByCategory(loginAccount.getFavouriteCategoryId());
                        session.setAttribute("myList", myList);
                    }

                    HttpServletRequest httpRequest = req;
                    String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                            + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                    LogService logService = new LogService();
                    //最后加入的信息“XXXXX”应当为该界面的信息以及一些商品信息
                    String logInfo = logService.logInfo(" ") + strBackUrl + "用户登录成功，回到主界面";
                    logService.insertLogInfo(loginAccount.getUsername(), logInfo);

                    resp.sendRedirect("mainForm");
                }
            }
        }

        private boolean validate () {
            if (this.username == null || this.username.equals("")) {
                this.msg = "用户名不能为空";
                return false;
            }
            if (this.password == null || this.password.equals("")) {
                this.msg = "密码不能为空";
                return false;
            }
            if (!this.checkCode.equalsIgnoreCase(checkCode1)){
                this.msg = "验证码错误";
                return false;
            }
            return true;
        }
    }

